package com.skytech.component.crawler.web;


import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.logger.LoggerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class LoggerController {
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    @Value("${elasticsearch.server}")
    private String server;

    @Autowired
    private LoggerService loggerService;

    private static final Logger log = LoggerFactory.getLogger(LoggerController.class);

    @PostMapping("log/es/getLogger")
    public JSONObject getLogger(@RequestBody JSONObject jsonObject) {
        ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + "crawler_news-52.200-log/_search",jsonObject, JSONObject.class);

        return loggerService.getLogger(responseEntity);

    }

    @PostMapping("log/es/getLoggerBy")
    public List<JSONObject> getLoggerBy(@RequestBody JSONObject jsonObject) {
        ResponseEntity<JSONObject> responseEntity = restTemplateBuilder.build().postForEntity(server + "crawler_news-52.200-log/_search",jsonObject, JSONObject.class);
        return loggerService.getLoggerBy(responseEntity);

    }
}
